##################################################################
###### Statistical analysis of trait - climate associations ######
##################################################################

setwd("C://Users/rober\Dropbox/2024_ThicketHydraulics_SkeltonButtnerPotts/1_data")

# Load Xylem Function Trait data from Choat et al. 2012
# Data for angiosperms ONLY!
data <- read.csv("07_Choat_etal2012.csv",header=T)

# Load Thicket data
summary.data <- read.csv("04b_OpticalVulnerabilityCurvesSummaryData.csv",header=T)

#####################################################
#### Visualising climate trait relationships for ####
################ different species ##################
#####################################################
# Plot trait (stem p50) against each climate variable (cwd, ppt) 
jpeg(filename = "Figure_4.jpg",
     width = 6, height = 6, units = "cm", pointsize = 1,
     bg = "white", res = 900, family = "", restoreConsole = TRUE,
     type = c("windows"),antialias = "cleartype")
par(mfrow=c(1,1),mar=c(14,8,1,1),oma=c(2,12,1,1),mgp=c(8,5,0)) 
# P50 and MAP from Choat et al. 2012 
# Angiosperms ONLY!
plot(data$p50~data$map, pch=16, col="lightgrey",xlim=c(0,4000),ylim=c(-12,0),xlab="",ylab="",yaxt="n",xaxt="n",lwd=0.5, cex=5)

# Thicket species
y <- summary.data$P50
x <- summary.data$ppt.50
l.mod <- lm(y~x)
summary(l.mod)
axis(side=1, labels=T, tick=T,cex.axis=4,lwd=0.5,tcl=-1.5)
axis(side=2, labels=T, tick=T,las=1,cex.axis=4,lwd=0.5,tcl=-1.5)
data$p50[which(data$p50 < min(summary.data$P50) )]
summary.data.n[,3] <- data$p50[which(data$p50 < min(summary.data$P50) )]
points(summary.data.n$P50~summary.data.n$ppt.50,pch=16,col="lightgrey",cex=5)
points(summary.data$P50~summary.data$ppt.50,pch=21,col="black",bg="steelblue",cex=8, lwd=0.3)
# Which genera are more resistant than the Thicket species?
data[which(data$P50 < min(summary.data$P50) ),1]

## Adenostoma (Chaparral)
## Coenothus x3 species (Chaparral)
## Ceratonia (Mediterranean)
## Crataegus (Mediterranean)
## Ligustrum (Mediterranean)
## Myrtus (Mediterranean)
## Olea (Mediterranean)
## Passerina (Cape)
## Paxistima (California)
## Phillyrea (Mediterranean)
## Pistacia
## Pteronia (Cape)
## Rhamnus x2 species 
## Rosmarinus
## Salvia mellifera

# fourteen genera of angiosperms

mtext(side=1,text="MAP (mm)",outer=F,line=12,at=2000,cex = 7)
mtext(expression(paste(P[50]," (MPa)")), side=2, line=10, outer=F, at=-6,cex=8)
# mtext(side=2,outer=T,at=0.5,line=6, LETTERS[2],cex=8,font=2,las=1)
legend(x=2000,y=-9,pch=c(21,16),pt.bg=c("steelblue","lightgrey"),col=c("black","lightgrey"),pt.cex=6,cex=6,pt.lwd=0.5,c("Thicket", "Choat et al."))
dev.off()
